package com.weimob.mcloud.wechatwork.customer.export;

import com.alibaba.dubbo.config.annotation.Reference;
import com.weimob.mcloud.wechatwork.common.utils.JsonUtil;
import com.weimob.mcloud.wechatwork.core.base.SingleRequest;
import com.weimob.mcloud.wechatwork.core.entity.privatesphere.PrivateSphere;
import com.weimob.mcloud.wechatwork.core.entity.relation.customer.CustomerStageRelation;
import com.weimob.mcloud.wechatwork.core.service.customer.CustomerProcessService;
import com.weimob.mcloud.wechatwork.core.service.customer.CustomerStageRelationService;
import com.weimob.mcloud.wechatwork.core.service.customer.request.CreateCustomerProcessRequest;
import com.weimob.mcloud.wechatwork.core.service.customer.request.DeletedCustomerProcessRequest;
import com.weimob.mcloud.wechatwork.core.service.customer.request.GetSingleCustomerProcessRequest;
import com.weimob.mcloud.wechatwork.core.service.customer.request.QueryCustomerProcessRequest;
import com.weimob.mcloud.wechatwork.core.service.customer.response.CreateCustomerProcessResponse;
import com.weimob.mcloud.wechatwork.core.service.customer.response.DeletedCustomerProcessResponse;
import com.weimob.mcloud.wechatwork.core.service.customer.response.GetSingleCustomerProcessResponse;
import com.weimob.mcloud.wechatwork.core.service.customer.response.QueryCustomerProcessResponse;
import com.weimob.mcloud.wechatwork.customer.convertor.customer.CustomerProcessConvert;
import com.weimob.mcloud.wechatwork.customer.convertor.customer.CustomerProcessRequestBuilder;
import com.weimob.mcloud.wechatwork.customer.facade.impl.process.CustomerStageRelationFacade;
import com.weimob.mcloud.wechatwork.customer.service.user.module.dto.customer.process.dto.CustomerProcessBaseDTO;
import com.weimob.mcloud.wechatwork.customer.service.user.module.dto.customer.process.dto.CustomerProcessDetailDTO;
import com.weimob.mcloud.wechatwork.customer.service.user.module.dto.customer.process.request.*;
import com.weimob.mcloud.wechatwork.customer.service.user.module.dto.customer.process.response.*;
import com.weimob.mcloud.wechatwork.customer.service.user.service.CustomerProcessExport;
import com.weimob.mcloud.wechatwork.customer.service.user.utils.SoaResponseUtil;
import com.weimob.mcloud.wechatwork.customer.third.PrivateSphereThird;
import com.weimob.soa.common.response.SoaResponse;
import com.weimob.soa.common.utils.SoaUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 *
 *
 * @author jiawei.you01
 * @date 2022/9/13 14:26
 * @since 1.0
 */
@Service
@Slf4j
public class CustomerProcessExportImpl implements CustomerProcessExport {

    @Resource
    CustomerProcessService customerProcessService;

    @Resource
    CustomerProcessRequestBuilder requestBuilder;

    @Resource
    CustomerProcessConvert customerProcessConvert;

    @Reference
    CustomerStageRelationService customerStageRelationService;

    @Autowired
    private PrivateSphereThird privateSphereThird;

    @Resource
    CustomerStageRelationFacade customerStageRelationFacade;




    @Override
    public SoaResponse<CheckProcessNameLegalResponse,Void> checkName(@Valid CheckProcessNameLegalRequest request) {
        log.info("开始校验名称合法性,request:{}", JsonUtil.obj2Str(request));
        PrivateSphere privateSphere = privateSphereThird.fetch(request.getBasicInfo(), true);
        //构建参数
        SingleRequest<GetSingleCustomerProcessRequest> singleRequest = requestBuilder.buildGetSingleRequest(privateSphere, request.getName(), null);

        //查询该名称已存在
        SoaResponse<GetSingleCustomerProcessResponse, Void> single = customerProcessService.getSingle(singleRequest);
        GetSingleCustomerProcessResponse getSingleCustomerProcessResponse = SoaUtil.unpack(single);
        CheckProcessNameLegalResponse response = new CheckProcessNameLegalResponse();

        //如果已经存在则校验不通过，返回false
        if(getSingleCustomerProcessResponse != null && getSingleCustomerProcessResponse.getCustomerProcess()!= null){
            response.setResult(false);
        }else {
            response.setResult(true);
        }

        return SoaResponseUtil.ok(response);
    }

    @Override
    public SoaResponse<SaveCustomerProcessResponse,Void> save(@Valid SaveCustomerProcessRequest request) {
        log.info("开始创建客户流程,request:{}", JsonUtil.obj2Str(request));

        //构建请求参数
        PrivateSphere privateSphere = privateSphereThird.fetch(request.getBasicInfo(), true);
        SingleRequest<CreateCustomerProcessRequest> createRequest = requestBuilder.buildCreateRequest(privateSphere,
                request.getProcessName(),
                request.getStatus(),
                request.getCustomerStage(),
                request.getProcessAuthList());

        //请求core创建
        SoaResponse<CreateCustomerProcessResponse, Void> response = customerProcessService.create(createRequest);
        CreateCustomerProcessResponse unpack = SoaUtil.unpack(response);

        //结果返回
        SaveCustomerProcessResponse saveCustomerProcessResponse = new SaveCustomerProcessResponse();
        saveCustomerProcessResponse.setProcessId(unpack.getProcessId());

        return SoaResponseUtil.ok(saveCustomerProcessResponse);
    }

    @Override
    public SoaResponse<UpdateCustomerProcessResponse,Void> update(@Valid UpdateCustomerProcessRequest request) {

        log.info("开始更新客户流程,request:{}", JsonUtil.obj2Str(request));

        //构建请求参数
        PrivateSphere privateSphere = privateSphereThird.fetch(request.getBasicInfo(), true);

        SingleRequest<com.weimob.mcloud.wechatwork.core.service.customer.request.UpdateCustomerProcessRequest> singleRequest =
                requestBuilder.buildUpdateRequest(privateSphere,
                        request.getProcessId(),
                        request.getProcessName(),
                        request.getStatus(),
                        request.getCustomerStage(),
                        request.getProcessAuthList());


        SoaResponse<com.weimob.mcloud.wechatwork.core.service.customer.response.UpdateCustomerProcessResponse, Void> update = customerProcessService.update(singleRequest);
        com.weimob.mcloud.wechatwork.core.service.customer.response.UpdateCustomerProcessResponse unpack = SoaUtil.unpack(update);
        List<String> deletedStageIdList = update.getResponseVo().getDeletedStageIdList();

        if(!CollectionUtils.isEmpty(deletedStageIdList)){
            //删除ES中的流程关系
            for (String stageId : deletedStageIdList) {
                customerStageRelationFacade.delProcessNoticeSearch(privateSphere,request.getProcessId(),stageId);
            }
        }



        UpdateCustomerProcessResponse response = new UpdateCustomerProcessResponse();
        response.setProcessId(request.getProcessId());

        return SoaResponseUtil.ok(response);
    }

    @Override
    public SoaResponse<UpdateProcessStatusResponse, Void> updateProcessStatus(UpdateProcessStatusRequest request) {

        PrivateSphere privateSphere = privateSphereThird.fetch(request.getBasicInfo(), true);
        SingleRequest<com.weimob.mcloud.wechatwork.core.service.customer.request.UpdateProcessStatusRequest> singleRequest = requestBuilder.buildUpdateStatusRequest(privateSphere,
                request.getProcessId(),request.getStatus());
        SoaResponse<com.weimob.mcloud.wechatwork.core.service.customer.response.UpdateProcessStatusResponse, Void> response = customerProcessService.updateProcessStatus(singleRequest);
        SoaUtil.unpack(response);
        return SoaUtil.ok(new UpdateProcessStatusResponse());
    }

    @Override
    public SoaResponse<QueryCustomerProcessListResponse,Void> queryList(@Valid QueryCustomerProcessListRequest request) {

        log.info("开始查询所有客户流程,request:{}", JsonUtil.obj2Str(request));

        //组装参数
        PrivateSphere privateSphere = privateSphereThird.fetch(request.getBasicInfo(), true);

        SingleRequest<QueryCustomerProcessRequest> singleRequest = requestBuilder.buildQueryList(privateSphere);

        //请求core
        SoaResponse<QueryCustomerProcessResponse, Void> soaResponse = customerProcessService.queryAll(singleRequest);
        QueryCustomerProcessResponse unpack = SoaUtil.unpack(soaResponse);
        if(CollectionUtils.isEmpty(unpack.getProcessList())){
            return SoaResponseUtil.ok(new QueryCustomerProcessListResponse());
        }

        //返回参数转换
        List<CustomerProcessBaseDTO> baseDTOS = customerProcessConvert.toCustomerProcessBaseDTOList(unpack.getProcessList());

        QueryCustomerProcessListResponse response = new QueryCustomerProcessListResponse();
        response.setCustomerProcessList(baseDTOS);

        return SoaResponseUtil.ok(response);
    }

    @Override
    public SoaResponse<GetSpecifiedCustomerProcessResponse,Void> getSpecified(@Valid GetSpecifiedCustomerProcessRequest request) {

        log.info("开始查询指定客户流程,request:{}", JsonUtil.obj2Str(request));

        //组装参数
        PrivateSphere privateSphere = privateSphereThird.fetch(request.getBasicInfo(), true);
        SingleRequest<GetSingleCustomerProcessRequest> singleRequest = requestBuilder.buildGetSingleRequest(privateSphere, null, request.getProcessId());

        //请求core
        SoaResponse<GetSingleCustomerProcessResponse, Void> single = customerProcessService.getSingle(singleRequest);
        GetSingleCustomerProcessResponse unpack = SoaUtil.unpack(single);

        //转换参数
        CustomerProcessDetailDTO customerProcessDetailDTO = customerProcessConvert.toCustomerProcessDetailDTO(unpack.getCustomerProcess());
        GetSpecifiedCustomerProcessResponse response = new GetSpecifiedCustomerProcessResponse();
        response.setDetailVO(customerProcessDetailDTO);

        return SoaResponseUtil.ok(response);
    }

    @Override
    public SoaResponse<GetCustomerCountByStageResponse, Void> getCustomerCountByStage(@Valid GetCustomerCountByStageRequest request) {

        PrivateSphere privateSphere = privateSphereThird.fetch(request.getBasicInfo(), true);

        SingleRequest<CustomerStageRelation> singleRequest = requestBuilder.buildFetchRelationCountRequest(privateSphere,request.getProcessId(),request.getStageId());

        SoaResponse<Integer, ?> fetchRelationCount = customerStageRelationService.fetchRelationCount(singleRequest);

        Integer totalCount = SoaUtil.unpack(fetchRelationCount);

        GetCustomerCountByStageResponse response = new GetCustomerCountByStageResponse();
        response.setTotalCount(totalCount);

        return SoaResponseUtil.ok(response);
    }

    @Override
    public SoaResponse<GetCustomerCountByProcessResponse,Void> getCustomerCountByProcess(@Valid GetCustomerCountByProcessRequest request) {

        PrivateSphere privateSphere = privateSphereThird.fetch(request.getBasicInfo(), true);

        SingleRequest<CustomerStageRelation> singleRequest = requestBuilder.buildFetchRelationCountRequest(privateSphere,request.getProcessId(), null);

        SoaResponse<Integer, ?> fetchRelationCount = customerStageRelationService.fetchRelationCount(singleRequest);

        Integer totalCount = SoaUtil.unpack(fetchRelationCount);

        GetCustomerCountByProcessResponse response = new GetCustomerCountByProcessResponse();
        response.setTotalCount(totalCount);

        return SoaResponseUtil.ok(response);
    }

    @Override
    public SoaResponse<DeleteCustomerProcessResponse,Void> deleted(@Valid DeleteCustomerProcessRequest request) {

        log.info("开始删除客户流程,request:{}", JsonUtil.obj2Str(request));

        //构建参数
        PrivateSphere privateSphere = privateSphereThird.fetch(request.getBasicInfo(), true);
        SingleRequest<DeletedCustomerProcessRequest> singleRequest = requestBuilder.buildDeletedRequest(privateSphere,request.getProcessId());
        //请求core
        SoaResponse<DeletedCustomerProcessResponse, Void> deleted = customerProcessService.deleted(singleRequest);
        SoaUtil.unpack(deleted);
        //删除流程关系
        customerStageRelationFacade.delProcessNoticeSearch(privateSphere,request.getProcessId(),null);
        DeleteCustomerProcessResponse response = new DeleteCustomerProcessResponse();
        response.setResult(true);
        return SoaResponseUtil.ok(response);
    }
}
